package com.lc.mqtt.controller;

import com.lc.mqtt.service.MqttMessageService;
import com.study.common.constant.MqttConstant;
import com.study.common.entity.Entity;
import com.study.common.entity.dto.MqttMessageDto;
import com.study.common.res.ApiResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @description: some desc
 * @author: LChuang
 * @date: 2023/11/13 14:48
 */
@RestController
@RequestMapping("/mqtt")
public class MqttController {

    @Resource
    private MqttMessageService mqttMessageService;

    @PostMapping("/sendPart")
    public ApiResult sendPart(@RequestBody MqttMessageDto<Entity> dto){
        MqttMessageDto<Entity> messageDto = new MqttMessageDto<>();
        messageDto.setTopic(MqttConstant.PART_TOPIC);
        messageDto.setContent("测试mqtt消息");
        messageDto.setDate(LocalDateTime.now());
        messageDto.setData(Entity.builder().name("jack").age(18).build());
        mqttMessageService.sendPartMessage(dto);
        return ApiResult.success();
    }

    @PostMapping("/sendGlobal")
    public ApiResult sendGlobal(@RequestBody MqttMessageDto<Entity> dto){
        MqttMessageDto<Entity> messageDto = new MqttMessageDto<>();
        messageDto.setTopic(MqttConstant.PART_TOPIC);
        messageDto.setContent("测试mqtt消息");
        messageDto.setDate(LocalDateTime.now());
        messageDto.setData(Entity.builder().name("jack").age(18).build());
        mqttMessageService.sendGlobalMessage(dto);
        return ApiResult.success();
    }

}
